package org.blogsite.greencode.struts2.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.blogsite.greencode.bean.SystemBulletinBoard;
import org.blogsite.greencode.bean.User;
import org.blogsite.greencode.service.SystemService;

import com.googlecode.sslplugin.annotation.Secured;
import com.opensymphony.xwork2.ActionSupport;
@Secured
public class SystemAction extends ActionSupport {

	private User user;
	private SystemService systemService;
	HttpServletRequest hs;
	private SimpleDateFormat dateformat;
	private SystemBulletinBoard systemBulletinBoard;
	
	
	public String execute() throws Exception {
		Log log=LogFactory.getLog(SystemAction.class);
		log.debug("debug");
//		systemService.save(user);
		return SUCCESS;
	}
	/*注册用户,而且必须先验证,注册信息是否合法*/
	public String register() throws Exception{
		/*调用服务层,验证注册信息是否合法*/
		hs=ServletActionContext.getRequest();
		try {
			/*调用服务层,验证提供的信息合法性*/
			boolean check=systemService.checkRegisterInfo(user.getName(), user.getJob_num());
			if(check){
				/*如果用户名不可用,返回*/
				if(systemService.checkUsername(user.getUsername())){
					hs.setAttribute("checkusernamefailure", "1");
					return "FAILURE_CHECKU_SERNAME";
				}
				user.setLastDate(getDateformat().format(new Date()));
				/*
				 * 密码jasypt加密存储
				 * */
				user.setPassword(systemService.encryptPassword(user.getPassword()));
				user.setType("instructor");
				user.setLastIP(hs.getRemoteAddr());
				systemService.save(user);
				return "SUCCESS_SAVE";
			}else{
				hs.setAttribute("checkfailure", "1");
				return "FAILURE_CHECK";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	/*用户登录验证*/
	public String login() throws Exception{
		
		hs=ServletActionContext.getRequest();
		String plainPassword=user.getPassword();
		try {
			User userLogin=systemService.checkLogin(user);
			if(userLogin==null) return "FAILURE_LOGIN";
			/*jasypt验证密码正确性*/
			boolean checkLogin=systemService.checkPassword(plainPassword, userLogin.getPassword());
			if(checkLogin){
				/*放入session*/
				hs.getSession().setAttribute("SESSION_USERNAME", userLogin.getUsername());
				hs.getSession().setAttribute("SESSION_USERTYPE", userLogin.getType());
//			String jsun=systemService.encryptPassword("wuzhijie");
//			System.out.println(jsun);
//			System.out.println(systemService.checkPassword("wuzhijie1", jsun));
				
//			System.out.println("session:"+hs.getSession().getAttribute("u_id"));
//			userLogin.setLastDate(getDateformat().format(new Date()));
//			userLogin.setLastIP(hs.getRemoteAddr());
				systemService.updateLastDateAndIP(userLogin);
				
				//获取所有公告
				hs.setAttribute("allBulletinList", systemService.displayAllBulletin());
				hs.setAttribute("searchsuccess", "1");
				/*判断该用户,该返回的视图类型.由于登录时获取不到类型,只能从
				 * 数据库中取得,所以先用判断,再者在拦截器控制
				 * */
				if("manager".equals(userLogin.getType())){
					return "SUCCESS_LOGIN_MANAGER";
				}else if("supporter".equals(userLogin.getType())){
					return "SUCCESS_LOGIN_SUPPORTER";
				}else
					return "SUCCESS_LOGIN_INSTRUCTOR";
			}else
				return "FAILURE_LOGIN";
			
		} catch (Exception e) {
			e.printStackTrace();
			return "FAILURE_LOGIN";
		}
	}
	/*查询用户个人信息*/
	public String searchPersonInformation() throws Exception{
		hs=ServletActionContext.getRequest();
		try {
			User userInfo=systemService.searchPersonInformation((String)hs.getSession().getAttribute("SESSION_USERNAME"));
			hs.setAttribute("userInfo",userInfo);
			hs.setAttribute("searchsuccess", "1");
			return "SUCCESS_SEARCH";
		} catch (Exception e) {
			e.printStackTrace();
			return "SUCCESS_SEARCH";
		}
	}
	
	/*显示修改用户密码*/
	public String displayModifyPassword() throws Exception{
		hs = ServletActionContext.getRequest();
		hs.setAttribute("displaymodify", "1");
		return "SUCCESS_DISPLAY";
	}
	/*修改用户密码*/
	public String modifyPassword() throws Exception{
		hs = ServletActionContext.getRequest();
		/*查询user实体放入 ognl ，以便更新数据*/
		System.out.println(user.getPassword()+user.getConfirm_password());
//		user=systemService.getUser((String)hs.getSession().getAttribute("SESSION_USERNAME"));
//		systemService.modifyPassword(user);
		hs.setAttribute("modifysuccess", "1");
		hs.setAttribute("searchsuccess", "1");
		return "SUCCESS_MODIFY_PASSWORD";
	}
	/*显示添加公告*/
	public String displayAddBulletin()throws Exception{
		hs = ServletActionContext.getRequest();
		hs.setAttribute("addbulletinboard", "1");
		hs.setAttribute("searchtitle", "1");
		return "SUCCESS_DISPLAY";
	}
	/*添加公告*/
	public String addBulletin()throws Exception{
		hs = ServletActionContext.getRequest();
		try {
			
			systemBulletinBoard.setUsername((String)hs.getSession().getAttribute("SESSION_USERNAME"));
			systemBulletinBoard.setUploadDate(getDateformat().format(new Date()));
			systemService.addBulletinBoard(systemBulletinBoard);
			hs.setAttribute("addbulletinboard", "1");
			hs.setAttribute("addbulletinboardsuccess", "1");
			hs.setAttribute("searchtitle", "1");
			return "SUCCESS_DISPLAY";
		} catch (Exception e) {
			e.printStackTrace();
			return "FAILURE_DISPLAY";
		}
	}
	/*显示详细公告*/
	public String detailBulletinBoard() throws Exception{
		hs = ServletActionContext.getRequest();
		try {
			
			SystemBulletinBoard bb= systemService.showDetailBulletinBoard(Integer.parseInt(hs.getParameter("id")));
			/*hs.setAttribute("show", "1");*/
			hs.setAttribute("detailbb", bb);
			hs.setAttribute("detailbulletinboardsuccess", "1");
			hs.setAttribute("searchtitle", "1");
			return "SUCCESS_MAINPAGE";
		} catch (Exception e) {
			e.printStackTrace();
			return "FAILURE_DISPLAY";
		}
	}
	
	/*显示主页*/
	public String mainpage() throws Exception{
		hs = ServletActionContext.getRequest();
		//获取所有公告
		try {
			System.out.println("asdf");
			hs.setAttribute("allBulletinList", systemService.displayAllBulletin());
			hs.setAttribute("searchsuccess", "1");
			return "SUCCESS_MAINPAGE";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	/*显示所有公告*/
	public String displayAllBulletin() throws Exception{
		hs = ServletActionContext.getRequest();
		try {
			//获取所有公告
			hs.setAttribute("allBulletinList", systemService.displayAllBulletin());
			hs.setAttribute("searchsuccess", "1");
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "succss";
	}
	
	
	
	

	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public SystemService getSystemService() {
		return systemService;
	}
	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}
	public SimpleDateFormat getDateformat() {
		return dateformat;
	}
	public void setDateformat(SimpleDateFormat dateformat) {
		this.dateformat = dateformat;
	}
	public SystemBulletinBoard getSystemBulletinBoard() {
		return systemBulletinBoard;
	}
	public void setSystemBulletinBoard(SystemBulletinBoard systemBulletinBoard) {
		this.systemBulletinBoard = systemBulletinBoard;
	}
	

}
